//
//  CHFileManagerExtensions.m
//  POBase
//
//  Created by Pieter Omvlee on 3/9/08.
//  Copyright 2008 Bohemian Coding. All rights reserved.
//

#import "CHFileManagerExtensions.h"


@implementation NSFileManager (CHFileManagerExtensions)

- (NSArray *)visibleSubpathsOfDirectory:(NSString *)basePath;
{
  BOOL isDir = NO;
  NSMutableArray *result = [NSMutableArray array];
  if ([self fileExistsAtPath:basePath isDirectory:&isDir] && isDir) {
    NSArray *contents = [self subpathsOfDirectoryAtPath:basePath error:NULL];
    
    for (NSString *path in contents)
      if ([path characterAtIndex:0] != '.')
        [result addObject:[basePath stringByAppendingPathComponent:path]];
    return result;
  } else
    [result addObject:basePath];
  return result;
}

- (NSArray *)visibleSubpathsOfDirectories:(NSArray *)paths
{
  NSMutableArray *result = [NSMutableArray array];
  
  for (NSString *path in paths)
    [result addObjectsFromArray:[self visibleSubpathsOfDirectory:path]];
  
  return result;
}

- (BOOL)createDirectoryIfNecessary:(NSString *)directory
{
  BOOL isDir = NO;
  if ([self fileExistsAtPath:directory isDirectory:&isDir] && isDir) return YES;
  if ([self fileExistsAtPath:directory isDirectory:&isDir] && !isDir) return NO;
  
  [self createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];
  return YES;
}

@end
